<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\service\AdminService;
use app\admin\service\RoleService;
use app\Request;
use think\facade\View;

class Admin extends AdminController
{
    /**
     * 管理员列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @return void
     */
    public function index(Request $request){
        if($request->param('_action') == 'getAdminList'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $adminList = AdminService::getAdminList($params,$page,$limit);
            return json($adminList);
        }
        $roleList = RoleService::getRoleList()['data'];
        return view::fetch('index',[
            'roleList' => $roleList
        ]);
    }

    /**
     * 管理员添加
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function add(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = AdminService::addOrEditData($params);
            return json($result);
        }
        $roleList = RoleService::getRoleList()['data'];
        return View::fetch('add',[
            'roleList' => $roleList
        ]);
    }

    /**
     * 管理员编辑
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function edit(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = AdminService::addOrEditData($params);
            return json($result);
        }
        $roleList = RoleService::getRoleList()['data'];
        $adminId = $request->param('id',0);
        $adminDetail = AdminService::getAdminDetail($adminId);
        return View::fetch('edit',[
            'roleList' => $roleList,
            'adminDetail' => $adminDetail
        ]);
    }

    /**
     * 管理员删除
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function delete(Request $request){
        $roleId = $request->param('id',0);
        if(!$roleId){
            return self::createFail('参数错误');
        }
        $result = AdminService::adminDelete($roleId);
        return json($result);
    }

    /**
     * 基本信息
     *
     * @Author tanruizheng
     * @DateTime 2021-07-15
     * @return void
     */
    public function myBasicInfo(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $params['id'] =  AdminService::instance()->getAdminInfo()['id'];
            $result = AdminService::updateBasicInfo($params);
            return json($result);
        }
        $myBasicInfo = AdminService::instance()->getAdminInfo();
        return view::fetch('myBasicInfo',[
            'myBasicInfo' => $myBasicInfo
        ]);
    }

    /**
     * 修改密码
     *
     * @Author tanruizheng
     * @DateTime 2021-07-15
     * @return void
     */
    public function changePassword(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = AdminService::changePassword($params);
            if($result['status']){
                session(null);
                return json(self::createSuccess('修改成功',[],200,url('Login/login')));
            }else{
                return json($result);
            }
            return json($result);
        }
        return view::fetch('changePassword');
    }
}
